package com.cfl.shiro;

/**
 * 身份认证
 */
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class ShiroJdbcRealm {
	
		@SuppressWarnings("unused")
		public static void main(String[] args) {

			// 读取配置文件，初始化SecurityManager工厂
			Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro_jdbc_realm.ini");
			
			// 获取SecurityManager实例
			SecurityManager securityManager = factory.getInstance();
			
			// 把securityManager实例绑定到SecurityUtils
			SecurityUtils.setSecurityManager(securityManager);
			
			// 得到当前执行的用户
			Subject subject = SecurityUtils.getSubject();
		
			// 创建Token令牌，用户名和密码
			UsernamePasswordToken token = new UsernamePasswordToken("chenfenli", "123456");
			
			// 身份认证
			try {
				subject.login(token);
				System.out.println("身份认证成功");
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("身份认证失败");
			}
		}
	
}
